import axios from 'axios'
import commonUrl from '../common/js/commonUrl.js'
import {loadOpenId, loadVillageId, loadUserId} from '../common/js/cache.js'
import qs from 'qs'
import {IsWeiXin} from '../common/js/isWeixin'

const url = `${commonUrl.apihost}index.php/home/`

//  头像资料数据
export function getMyInfo() {
  const type = IsWeiXin() ? 'WXWAP' : ''
  const url = `${commonUrl.apihost}index.php/home/login/getUser`
  const data = {
    openid: loadOpenId() ? loadOpenId() : '',
    userId: loadUserId(),
    type: type
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 获取验证码
export function getPhoneCode(phoneNumber) {
  return axios({
    method: 'GET',
    url: `${url}login/code?phone=${phoneNumber}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 保存编辑资料
export function saveEditData(name, realname, phone, sex, identity, code, email) {
  const data = {
    name,
    real_name: realname,
    phone,
    sex,
    wx_role: identity,
    village_id: loadVillageId(),
    code,
    userId: loadUserId(),
    email
  }
  return axios({
    method: 'POST',
    url: `${url}login/perfection`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 我的房列表
export function getDatas() {
  const url = `${commonUrl.apihost}/index.php/home/house_Auth/getMyHouse`
  const data = {
    userId: loadUserId()
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 房屋认证  小区  苑  幢  单元  房号
export function getHouseData(id) {
  return axios({
    method: 'GET',
    url: `${url}house_Auth/getHouse?id=${id}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 保存房屋资料
export function saveHouseData(houseid, areaid, phone, codeNum) {
  const data = {
    user_id: loadUserId(),
    house_id: houseid,
    areas_id: areaid,
    phone: phone,
    code: codeNum
  }
  return axios({
    method: 'POST',
    url: `${url}house_Auth/setHouse`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 我的查验列表
export function getCheckList() {
  return axios({
    method: 'GET',
    url: `${url}examine/myExamineIndex/?user_id=${loadUserId()}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 删除某条查验内容
export function delCheck(id) {
  return axios({
    method: 'GET',
    url: `${url}examine/deleteExamine/?id=${id}`
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 编辑查验内容
export function editCheckContent(id, categoryid) {
  const data = {
    id: id,
    category_id: categoryid
  }
  return axios({
    method: 'POST',
    url: `${url}examine/firstAssortInfo`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 编辑查验内容
export function editCheckEvery(id, categoryid) {
  const data = {
    id: id,
    category_id: categoryid
  }
  return axios({
    method: 'POST',
    url: `${url}examine/secondLowCateInfo`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 提交编辑查验内容
export function submitEditCheckEvery(id, summary) {
  const data = {
    related_id: id,
    summary: summary
  }
  return axios({
    method: 'POST',
    url: `${url}examine/editSummary`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 房屋认证的电话号码获取
export function getHousePhone(id) {
  const data = {
    houseId: id
  }
  return axios({
    method: 'POST',
    url: `${url}house_Auth/getHousePhone`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 房屋认证的电话号码验证码
export function HouseCode(phoneNumber) {
  const data = {
    userId: loadUserId(),
    phone: phoneNumber
  }
  return axios({
    method: 'POST',
    url: `${url}house_Auth/HouseCode`,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
